<%@page contentType="text/html; charset=utf-8"%>
<%@ include file="../inc/TagLibs.jsp"%>
<c:set var="pageTitle" value="应收款账龄分布表" />
<%@ include file="../inc/Title.jsp"%>
<head>
<link href="/css/Query.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="/css/BillList.css" />
<script src="/js/QueryList.js"></script>
<script src="/js/popup_ext.js"></script>
<script language="javascript">
	function createPageParaStr(){
		var str="est_name="+encodeURIComponent(document.getElementById("est_name").value);
		str=str+"&area_id="+document.getElementById("area_id").value;
		str=str+"&et_id="+document.getElementById("et_id").value;
		str=str+"&el_id="+document.getElementById("el_id").value;
		str=str+"&ts_id="+document.getElementById("ts_id").value;
		str=str+"&mng_id="+encodeURIComponent(document.getElementById("mng_id").value);
		str=str+"&mng_name="+encodeURIComponent(document.getElementById("mng_name").value);
		return str;
	}
	
	function createProgramParaStr(){
		var str="est_name="+encodeURIComponent(document.getElementById("hid_est_name").value);
		str=str+"&area_id=${areaId}";
		str=str+"&et_id=${etId}";
		str=str+"&el_id=${elId}";
		str=str+"&ts_id=${tsId}";
		str=str+"&mng_id=${mngId}";
		str=str+"&mng_name="+encodeURIComponent(document.getElementById("hid_mng_name").value);
		return str;
	}
	function doSearch(){
		var str;
		if(IsAdvSearch){//高级
			str="search_type=1";
			if(document.getElementById("cb_est").checked){
				var estName=document.getElementById("est_name").value.trim();
				if(estName==null || estName==""){
					ShowAlert('提示框','请输入客户名称',200,100);
					return;
				}
				str=str+"&cb_est=1";
			}
			if(document.getElementById("cb_area").checked){
				var areaId=document.getElementById("area_id").value;
				if(areaId==null || areaId=="" || areaId=="0"){
					ShowAlert('提示框','请选择地区',200,100);
					return;
				}
				str=str+"&cb_area=1";
			}
			if(document.getElementById("cb_et").checked){
				var etId=document.getElementById("et_id").value;
				if(etId==null || etId=="" || etId=="0"){
					ShowAlert('提示框','请选择客户类别',200,100);
					return;
				}
				str=str+"&cb_et=1";
			}
			if(document.getElementById("cb_el").checked){
				var elId=document.getElementById("el_id").value;
				if(elId==null || elId=="" || elId=="0"){
					ShowAlert('提示框','请选择客户等级',200,100);
					return;
				}
				str=str+"&cb_el=1";
			}
			if(document.getElementById("cb_ts").checked){
				var tsId=document.getElementById("ts_id").value;
				if(tsId==null || tsId=="" || tsId=="0"){
					ShowAlert('提示框','请选择帐套',200,100);
					return;
				}
				str=str+"&cb_ts=1";
			}
			if(document.getElementById("cb_mng").checked){
				var mngId=document.getElementById("mng_id").value;
				if(mngId==null || mngId=="" || mngId=="0"){
					ShowAlert('提示框','请选择业务员',200,100);
					return;
				}
				str=str+"&cb_mng=1";
			}
		}else{//快速
			str="search_type=2";
		}
		var paraStr=createPageParaStr();
		if(paraStr!=""){
			str=str+"&"+paraStr;
		}
		window.location="/business/ReceivableAgeList.ihtm?"+str;
	}
	function getSearchCondition(){
		var searchType="${searchType}";
		var str="search_type="+searchType;
		if(searchType=="1"){//高级
			<c:if test="${cbEst=='1'}">str=str+"&cb_est=1";</c:if>
			<c:if test="${cbArea=='1'}">str=str+"&cb_area=1";</c:if>
			<c:if test="${cbEt=='1'}">str=str+"&cb_et=1";</c:if>
			<c:if test="${cbEl=='1'}">str=str+"&cb_el=1";</c:if>
			<c:if test="${cbTs=='1'}">str=str+"&cb_ts=1";</c:if>
			<c:if test="${cbMng=='1'}">str=str+"&cb_mng=1";</c:if>
		}else{//快速
			
		}
		var paraStr=createProgramParaStr();
		if(paraStr!=""){
			str=str+"&"+paraStr;
		}
		return str;
	}
	function doAutoCheck(objId){
		if(document.getElementById(objId).style.visibility!="hidden"){
			document.getElementById(objId).checked=true;
		}
	}
	function DoubleClick(it){
		
	}
	function doDownload(){
		var str=getSearchCondition();
		window.location="/business/ReceivableAgeListData.ihtm?content_type=EXCEL&"+str;
	}
	function doCancel(){
		window.close();
	}
	function checkInput(){
		if(window.event.keyCode==13){
			doSearch();
		}
	}
	function selectStaffer(){
		var url="/base/StafferMainForSelect.ihtm";
		var cb="selectStafferCallBack()";
		ShowIframe("选择员工",url,800,500,cb);
	}
	function selectStafferCallBack(){
		if(popRetVal!=null){
			var arr=popRetVal.split(";");
			document.getElementById("mng_id").value=arr[0];
			document.getElementById("mng_name").value=arr[1];
			doAutoCheck("cb_mng");
		}
	}
	function doInit(){
		if(window.screen.availWidth>=1280){
			var w=window.screen.availWidth*0.98;
			window.resizeTo(w,600);
			//居中
			var resizeWinTop=(window.screen.availHeight-600)/2;
			var resizeWinLeft=(window.screen.availWidth-w)/2;
			window.moveTo(resizeWinLeft,resizeWinTop);
		}
		var monthArr=new Array();
		<c:forEach var="item" items="${monthArr}" varStatus="i">
			monthArr[${i.index}]='${item}';
		</c:forEach>
		var str=getSearchCondition();
		var url="/business/ReceivableAgeListData.ihtm?"+str;
		var xmlHttp=createXMLHttpRequest();
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4) {
				if (xmlHttp.status == 200) {
					var xml=xmlHttp.responseXML;
					var data=xml.getElementsByTagName("data");
					var tbl=document.getElementById("dataTbl");
					var trClass="query_list_data_tr1";
	      			for(var i=0;i<data.length;i++){
	      				
	      				var estId=getNodeValue(data[i],"est_id");
	      				var estName=getNodeValue(data[i],"est_name");
	      				var remCost=getNodeValue(data[i],"rem_cost");
	      				
	      				var tr=document.createElement("tr");
	      				tr.setAttribute("className",trClass);
	      				tr.setAttribute("class",trClass);
	      				tr.style.cursor="pointer";
	      				tr.setAttribute("index",i+1);
	      				tr.onclick=dataOnClick;
	      				tr.onmouseout=dataOnMouseOut;
	      				tr.onmouseover=dataOnMouseOver;
	      				
	      				var td;
	      				var input;
	      				
	      				td=document.createElement("td");
	      				input=document.createElement("input");
	      				input.setAttribute("type","radio");
	      				input.setAttribute("name","query_checkbox");
	      				input.setAttribute("value",estId);
	      				input.style.display="none";
	      				td.appendChild(input);
	      				td.appendChild(document.createTextNode(estName));
						tr.appendChild(td);
						
						td=document.createElement("td");
						td.innerHTML="<a href=\"javascript:showDetail('"+estName+"','');\">"+remCost+"</a>";
						//td.appendChild(document.createTextNode(remCost));
						tr.appendChild(td);
						
						var monCost;
						for(var j=0;j<12;j++){
							monCost=getNodeValue(data[i],"month"+j);
							td=document.createElement("td");
							td.innerHTML="<a href=\"javascript:showDetail('"+estName+"','"+monthArr[j]+"');\">"+monCost+"</a>";
							//td.appendChild(document.createTextNode(monCost));
							tr.appendChild(td);
						}
						
						var yeaCost=getNodeValue(data[i],"year_bef_cost");
						td=document.createElement("td");
						td.appendChild(document.createTextNode(yeaCost));
						tr.appendChild(td);
						
						tbl.appendChild(tr);
						
	      				if(trClass=="query_list_data_tr1"){
	      					trClass="query_list_data_tr2";
	      				}else{
	      					trClass="query_list_data_tr1";
	      				}
	      			}
	      			
	      			var total=xml.getElementsByTagName("total");
	      			
	      			var tr=document.createElement("tr");
	      			var th=document.createElement("th");
					th.appendChild(document.createTextNode("合计"));
					tr.appendChild(th);
					
					th=document.createElement("th");
					//th.innerHTML="<a href=\"javascript:showDetail('','');\">"+getNodeValue(total[0],"rem_cost")+"</a>";
					th.appendChild(document.createTextNode(getNodeValue(total[0],"rem_cost")));
					tr.appendChild(th);
	      			
	      			for(var j=0;j<12;j++){
						th=document.createElement("th");
						//th.innerHTML="<a href=\"javascript:showDetail('','"+monthArr[j]+"');\">"+getNodeValue(total[0],"month"+j)+"</a>";
						th.appendChild(document.createTextNode(getNodeValue(total[0],"month"+j)));
						tr.appendChild(th);
					}
					
					th=document.createElement("th");
					th.appendChild(document.createTextNode(getNodeValue(total[0],"year_bef_cost")));
					tr.appendChild(th);
	      			
	      			tbl.appendChild(tr);
	      			
	      			document.getElementById("waitDiv").style.display="none";
	      			document.getElementById("dataDiv").style.display="";
				}
			}
		};
		xmlHttp.send(null);
		
		document.getElementById("est_name").focus();
	}
	function getNodeValue(node,tagName){
		var children=node.childNodes;
		for(var j=0;j<children.length;j++){
			if(children[j].tagName==tagName){
				return children[j].firstChild.data;
			}
		}
		return "";
	}
	function dataOnClick(){
		ClickCheck(this);
	}
	function dataOnMouseOut(){
		mouseout(this);
	}
	function dataOnMouseOver(){
		mouseover(this);
	}
	function showDetail(estName,monthStr){
		var startDate=monthStr+"-01";
		var arr=monthStr.split("-");
		var d=new Date(arr[0],arr[1],1);
		d.setDate(d.getDate()-1);
		var endDate=d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
		var url="/business/BusinessBillListForReceive.ihtm?search_type=1";
		if(estName!=""){
			url=url+"&cb_est=1&est_name="+encodeURIComponent(estName);
		}
		if(monthStr!=""){
			url=url+"&cb_order_date=1&start_date="+startDate+"&end_date="+endDate;
		}
		<c:if test="${cbTs=='1'}">
			url=url+"&cb_ts=1&ts_id=${tsId}";
		</c:if>
		showWin(1024,600,url);
	}
</script>
</head>
<body oncontextmenu="return false">
<div class="Wrap"><!-- 页面主框架 -->
    
	<div id="ListContainer" class="Container"><!-- 主体容器 -->
	    
	    <!-- 标题栏 -->
	    <div class="Heading BillIcon">
	        <div class="HeadingContent">
	            <h1>应收款账龄分布表</h1>
	            <span class="Note">统计客户应收款的账龄分布</span>
	        </div>
	    </div>
	
		<!-- 主内容 -->
		<div class="Content">
	        <!-- 筛选控件栏 -->
	        <div class="FilterBar ClearFix">
	            <div class="SearchIcon"></div>
	            <span class="fl">
	            	<table class="MoreOption" cellpadding="0" cellspacing="0">
	            		<th class="c1" width="80px">
	            			<input type="checkbox" name="cb_est" id="cb_est" <c:if test="${cbEst=='1' || searchType=='2'}">checked="true"</c:if> style="visibility:hidden;" />
	            			客户名称
	            		</th>
	            		<td class="c3" width="260px">
	            			<input type="text" class="TextBox" id="est_name" name="est_name" onKeyUp="checkInput();" onFocus="doAutoCheck('cb_est');" value="<c:out value="${estName}" />" title="以客户名称、客户拼音字头进行模糊查询">
							<input type="hidden" id="hid_est_name" value="<c:out value="${estName}" />">
	            		</td>
	            	</table>
	            </span>
	            <span class="fr">
	            	<a href="javascript:;" id="lnkMoreOption">更多查询选项</a>
		  			<input type="button" value=" 查 询" onClick="doSearch();" id="sSearchButton" name="sSearchButton" class="buttom search-but">
		  			<input type="button" value=" 导 出" onClick="doDownload();" id="sDownButton" name="sDownButton" class="buttom  search-out" <c:if test="${currentUser.user.userType!=1 && fn:indexOf(currentUser.privs,';NO_EXPO;')!=-1}">disabled="true"</c:if>>
	            </span>
				<div id="dvMoreOptionBox" class="MoreOptionBox" style="display:none;">
					<table class="MoreOption" cellpadding="0" cellspacing="0">
						<tr>
							<th class="c1" width="80px">
			        			<input type="checkbox" name="cb_et" id="cb_et" <c:if test="${cbEt=='1'}">checked="true"</c:if>>
			        			客户类别
			        		</th>
			        		<td class="c1" width="260px">
			        			<select name="et_id" id="et_id" onFocus="doAutoCheck('cb_et');">
							  		<option value="0">==请选择==</option>
							  		<c:forEach var="item" items="${estTypeList}">
							  		<option value="${item.id}" <c:if test="${etId==item.id}">selected</c:if>>${item.typeName}</option>
							  		</c:forEach>
						  		</select>
			    			</td>
			    		</tr>
			    		<tr>
							<th class="c1">
			        			<input type="checkbox" name="cb_el" id="cb_el" <c:if test="${cbEl=='1'}">checked="true"</c:if>>
			        			客户等级
			        		</th>
			        		<td class="c1">
			        			<select name="el_id" id="el_id" onFocus="doAutoCheck('cb_el');">
							  		<option value="0">==请选择==</option>
							  		<c:forEach var="item" items="${estLevelList}">
							  		<option value="${item.id}" <c:if test="${elId==item.id}">selected</c:if>>${item.levelName}</option>
							  		</c:forEach>
						  		</select>
			    			</td>
			    		</tr>
			    		<tr>
							<th class="c1">
			        			<input type="checkbox" name="cb_area" id="cb_area" <c:if test="${cbArea=='1'}">checked="true"</c:if>>
			        			地区
			        		</th>
			        		<td class="c1">
			        			<select name="area_id" id="area_id" onFocus="doAutoCheck('cb_area');">
									<option value="0">==全部地区==</option>
									<commonTag:createSelectTree list="${areaList}" emptyOption="0" justEnabled="1" defaultValue="${areaId}" />
								</select>
			    			</td>
			    		</tr>
			    		<tr>
							<th class="c1">
			        			<input type="checkbox" name="cb_mng" id="cb_mng" <c:if test="${cbMng=='1'}">checked="true"</c:if>>
			        			业务员
			        		</th>
			        		<td class="c1">
			        			<input type="text" class="TextBox readonly" readonly="readonly" maxlength="50" name="mng_name" id="mng_name" onKeyUp="checkInput();" onFocus="doAutoCheck('cb_mng');" value="<c:out value="${mngName}" />" >
								<img class="imgIconButton" src="/images/make-a.gif" onClick="selectStaffer();" />
								<input type="hidden" id="mng_id" value="<c:out value="${mngId}" />">
								<input type="hidden" id="hid_mng_name" value="<c:out value="${mngName}" />">
			    			</td>
			    		</tr>
			    		<tr>
							<th class="c1">
			        			<input type="checkbox" name="cb_ts" id="cb_ts" <c:if test="${cbTs=='1'}">checked="true"</c:if>>
			        			账套
			        		</th>
			        		<td class="c1">
			        			<select name="ts_id" id="ts_id" onFocus="doAutoCheck('cb_ts');">
					  				<option value="0">==请选择账套==</option>
					  				<c:forEach var="item" items="${tsList}">
					  				<option value="${item.id}" <c:if test="${tsId==item.id}">selected</c:if>>${item.title}</option>
					  				</c:forEach>
				  				</select>
			    			</td>
			    		</tr>
					</table>
			    	
					<div class="OptionOrderBy">
	                    <span class="fr"><a href="javascript:;" id="lnkHiddenOption">隐藏查询选项</a></span>
	                </div>
				</div>
			</div>
			
			<!-- 提示信息显示栏 -->
	        <div class="MsgBar">
	            <%@include file="../inc/AlertMsg.jsp"%>
	        </div>
	        
	        <div id="waitDiv" style="height:100px">
		      	<table width="100%" height="100%">
		      		<tr>
		      			<td style="text-align:center;background-color:white;"><img src="/images/wait.gif"></td>
		      		</tr>
		      	</table>
		    </div>
	        
		    <div class="table-list" id="dataDiv" style="display:none">
		        <table width="100%">
					<tr>
						<th width="10%">客户名称</th>
						<th width="10%">欠款合计</td>
						<c:forEach var="item" items="${monthArr}">
							<th width="6%">${item}</th>
						</c:forEach>
						<th width="8%">1年前</th>
					</tr>
					<tbody id="dataTbl">
					</tbody>
				</table>
			</div>
			
		</div>
	</div>
</div>
</body>
</html>
<script type="text/javascript">
	function ChangeWindowSize( container, maxWidth ){
	    setTimeout( function(){
	        var w1 = document.documentElement.clientWidth-40;
	        if( w1>maxWidth ) w1 = maxWidth;
	        else if( w1<970 ) w1 = 960;
	        container.style.width = w1+'px';
	    }, 300 );
	}
	var IsAdvSearch = false;
	(function(){
		doInit();
	    var ListContainer = document.getElementById('ListContainer');
	    ChangeWindowSize( ListContainer, 2020 );    // 超过 1280 的最大 1260 ，不全部铺满
	
	    var lnkMoreOption   = document.getElementById('lnkMoreOption');
	    var lnkHiddenOption = document.getElementById('lnkHiddenOption');
	    lnkMoreOption.onclick = function(){
	    	IsAdvSearch = true;
	        document.getElementById('dvMoreOptionBox').style.display = 'block';
	        document.getElementById('cb_est').style.visibility = 'visible';
	        this.style.visibility = 'hidden';
	    };
	    lnkHiddenOption.onclick = function(){
	    	IsAdvSearch = false;
	        document.getElementById('dvMoreOptionBox').style.display = 'none';
	        document.getElementById('cb_est').style.visibility = 'hidden';
	        lnkMoreOption.style.visibility = 'visible';
	    };
	})();
</script>
